package com.spsoft.vatoutinv.service.custom.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spsoft.core.enums.EnableStatusEnum;
import com.spsoft.core.enums.ResponseEnum;
import com.spsoft.core.exception.BusinessException;
import com.spsoft.core.model.Result;
import com.spsoft.core.utils.BeanUtils;
import com.spsoft.core.utils.SecurityUtils;
import com.spsoft.core.utils.StringUtil;
import com.spsoft.vatoutinv.entity.custom.VmsCustomInfo;
import com.spsoft.vatoutinv.mapper.VmsCustomInfoMapper;
import com.spsoft.vatoutinv.model.dto.custom.VmsCustomInfoDto;
import com.spsoft.vatoutinv.model.dto.custom.VmsCustomInfoQueryDto;
import com.spsoft.vatoutinv.model.vo.vmscustom.VmsCustomInfoVO;
import com.spsoft.vatoutinv.service.custom.VmsCustomInfoService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * 客户基础信息;SEQ_VMS_CUSTOM_INFO 服务实现类
 * </p>
 *
 * @author dcy
 * @since 2024-01-02
 */
@RequiredArgsConstructor
@Service
public class VmsCustomInfoServiceImpl extends ServiceImpl<VmsCustomInfoMapper, VmsCustomInfo> implements VmsCustomInfoService {

    private final VmsCustomInfoMapper vmsCustomInfoMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Boolean> saveInfo(VmsCustomInfoDto dto) {
        VmsCustomInfo info = new VmsCustomInfo();
        BeanUtils.copyBeanProp(info, dto);
        info.setInfoStatus(EnableStatusEnum.Enable.getCode()); // 新增默认启用
        Date now = new Date();
        info.setCreatedBy(SecurityUtils.getUserCode());
        info.setCreatedTime(now);
        info.setUpdatedBy(SecurityUtils.getUserCode());
        info.setUpdatedTime(now);
        info.setUpdateDeptCode(SecurityUtils.getDeptCode());
        info.setUpdateUserName(SecurityUtils.getUsername());
        return Result.OK(this.save(info));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Boolean> updateInfo(VmsCustomInfoDto dto) {
        // 校验数据启用状态是否是启用状态
        VmsCustomInfo i = this.getById(dto.getId());
        if (i == null || !i.getInfoStatus().equals(EnableStatusEnum.Enable.getCode())) {
            return Result.ERROR(ResponseEnum.ILLEGAL_ARGUMENT_ERROR);
        }

        if (!i.getUpdateDeptCode().equals(SecurityUtils.getDeptCode())) {
            throw new BusinessException(ResponseEnum.ILLEGAL_ARGUMENT_ERROR.getCode(), "机构错误");
        }
        VmsCustomInfo info = new VmsCustomInfo();
        BeanUtils.copyBeanProp(info, dto);
        info.setInfoStatus(EnableStatusEnum.Enable.getCode()); // 新增默认启用
        Date now = new Date();
        info.setUpdatedBy(SecurityUtils.getUserCode());
        info.setUpdatedTime(now);
        return Result.OK(this.updateById(info));
    }

    @Override
    public Result<IPage<VmsCustomInfoVO>> getPage(VmsCustomInfoQueryDto dto) {
        LambdaQueryWrapper<VmsCustomInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.likeRight(StringUtil.isNotEmpty(dto.getHprmc()), VmsCustomInfo::getHprmc, dto.getHprmc())
                .eq(dto.getInfoStatus() != null , VmsCustomInfo::getInfoStatus, dto.getInfoStatus())
                .eq(VmsCustomInfo::getUpdateDeptCode, SecurityUtils.getDeptCode())
                .orderByDesc(VmsCustomInfo::getUpdatedTime);

        IPage<VmsCustomInfoVO> ip = this.page(new Page<>(dto.getPageNo(), dto.getPageSize()), wrapper)
                .convert(info -> {
                    VmsCustomInfoVO vo = new VmsCustomInfoVO();
                    BeanUtils.copyBeanProp(vo, info);
                    vo.setInfoStatusStr(info.getInfoStatus().equals(EnableStatusEnum.Enable.getCode())
                            ? EnableStatusEnum.Enable.getInfo() : "停用");
                    return vo;
                });
        return Result.OK(ip);
    }

    @Override
    public Result<VmsCustomInfoVO> detail(Long id) {
        VmsCustomInfoVO vo = vmsCustomInfoMapper.getInfoById(id);

        if (vo != null) {
            vo.setInfoStatusStr(vo.getInfoStatus().equals(EnableStatusEnum.Enable.getCode())
                    ? EnableStatusEnum.Enable.getInfo() : "停用");
            return Result.OK(vo);
        }
        return Result.ERROR(ResponseEnum.ILLEGAL_ARGUMENT_ERROR);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Boolean> disableById(Long id) {
        LambdaUpdateWrapper<VmsCustomInfo> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(VmsCustomInfo::getInfoStatus, EnableStatusEnum.Disabled.getCode())
                .eq(VmsCustomInfo::getId, id);
        return Result.OK(this.update(null, wrapper));
    }
}
